package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.api.domain.dto.CommentReplyInfoDTO;
import com.juyu.miliao.api.domain.dto.WhitePacketCommentReplyInfoDTO;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.api.service.CommentReplyInfoService;
import com.juyu.miliao.api.service.DynamicCommentInfoService;
import com.juyu.miliao.api.service.OpenApiService;
import com.juyu.miliao.api.service.whitepacket.WhitePacketCommentReplyInfoService;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.OpenApiStatus;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 回复controller
 *
 * @author xuanqicheng
 * @create 2019-21-15:31
 */
@RestController
@RequestMapping("/reply")
public class CommentReplyInfoController {

    @Autowired
    private CommentReplyInfoService commentReplyInfoService;

    @Autowired
    private OpenApiService openApiService;

    @Autowired
    private WhitePacketCommentReplyInfoService whitePacketCommentReplyInfoService;

    @Autowired
    private AppKeyService appKeyService;

    /**
     * 新增回复评论，目前是二级评论
     * @param token
     * @param commentReplyInfoDTO
     * @param bindingResult
     */
    @PostMapping("/")
    public void addCommentReply(HttpServletRequest request, Authentication token, @Validated CommentReplyInfoDTO commentReplyInfoDTO,
                                BindingResult bindingResult) throws IOException {

        Boolean bl = getAudit(request);

        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        Subject subject = (Subject) token.getPrincipal();

        if(!bl) {
            commentReplyInfoDTO.setReplyFromUserId(subject.getId());
            //检查用户是否达到发送评论限制
            Appkey appkey =appKeyService.getAppKeyByKey("commentReplyNumber");
            commentReplyInfoService.checkNumber(subject.getId(),Integer.parseInt(appkey.getValue()));
            commentReplyInfoService.addCommentReply(commentReplyInfoDTO);
        }else{
            WhitePacketCommentReplyInfoDTO whitePacketCommentReplyInfoDTO = new WhitePacketCommentReplyInfoDTO();
            BeanUtils.copyProperties(commentReplyInfoDTO,whitePacketCommentReplyInfoDTO);

            whitePacketCommentReplyInfoDTO.setReplyFromUserId(subject.getId());

            whitePacketCommentReplyInfoService.addCommentReply(whitePacketCommentReplyInfoDTO);

        }

    }


    /**
     * 获取审核状态
     * @param request
     * @return
     */
    private Boolean getAudit(HttpServletRequest request) {
        CommonHead commonHead = VerifyUtil.verifyCommonHead(request);
        OpenApiStatus ops = openApiService.getOpenApi(commonHead);
        if(ops != null && ops.getForwardAudit() && ops.getBackAudit()){
            return true;
        }else{
            return false;
        }
    }
}
